<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:if test="${totalNums > 0}">
  <script>
      function redirectPages(toPage) {
        var url = window.location.toString();
        if (url.indexOf('page=') != -1) {
          url = url.replace(/page=[0-9]+/, 'page=' + toPage);
        } else {
          if (url.indexOf('?') == -1) {
            url += '?page=' + toPage;
          } else {
            url += '&page=' + toPage;
          }
        }
        window.location.href = url;
      }
    </script>

  <c:set var="totalNums" value="${requestScope.pageConfig.total}" />
  <c:set var="totalPages" value="${requestScope.pageConfig.totalPages}" />
  <c:set var="page" value="${empty requestScope.pageConfig.currentPage || requestScope.pageConfig.currentPage == 0 ? 1 : requestScope.pageConfig.currentPage}" />


  <ul class="pagination pull-right" style="margin-top: 0;">
    <c:if test="${page > 1}">
      <li><a href="javascript:redirectPages('${page -1}')">Previous</a></li>
    </c:if>
    <c:forEach begin="1" end="${totalPages}" var="i">
      <c:if test="${(i <= 2)}">
        <c:choose>
          <c:when test="${page == i}">
            <li class="disabled"><span style="color: red">${i}</span></li>
          </c:when>
          <c:otherwise>
            <li><a href="javascript:redirectPages('${i}')">${i}</a></li>
          </c:otherwise>
        </c:choose>
      </c:if>

      <c:if test="${(i >= (page - 2) && i <= (page + 2) && (i < totalPages - 1)) && i > 2}">
        <c:if test="${prePoint == null}">
          <c:if test="${(i - 2) > 1}">
            <li class="disabled"><span>...</span></li>
            <c:set var="prePoint" value="1" />
          </c:if>
        </c:if>
        <c:set var="prePoint" value="1" />

        <c:choose>
          <c:when test="${page == i}">
            <li class="disabled"><span style="color: red">${i}</span></li>
          </c:when>
          <c:otherwise>
            <li><a href="javascript:redirectPages('${i}')">${i}</a></li>
          </c:otherwise>
        </c:choose>
        <c:set var="lastPageInRegion" value="${i}" />
      </c:if>

      <c:if test="${lastPoint == null}">
        <c:if test="${lastPageInRegion != null}">
          <c:if test="${((i - 1) > lastPageInRegion) && i <= totalPages - 1}">
            <li class="disabled"><span>...</span></li>
            <c:set var="lastPoint" value="1" />
          </c:if>
        </c:if>
      </c:if>

      <c:if test="${(i >= totalPages - 1) && (i > 2)}">
        <c:choose>
          <c:when test="${page == i}">
            <li class="disabled"><span style="color: red">${i}</span></li>
          </c:when>
          <c:otherwise>
            <li><a href="javascript:redirectPages('${i}')">${i}</a></li>
          </c:otherwise>
        </c:choose>
      </c:if>
    </c:forEach>

    <c:if test="${page <= (totalPages - 1)}">
      <li><a href="javascript:redirectPages('${page + 1}')">Next</a></li>
    </c:if>
  </ul>
</c:if>
